<?

	require_once("fta.201003310000/fta.php");
	fta_registerModules(
		array(
		//"cms.201003310000/config/cmsConfig",//we will deprecate the old cms config soon..
		//"FTAWebPage/ftaDefaultWidgets",
			
		"FTACart/FTAOrderItem",
		"FTACart/FTAOrderContainer",			
		//"FTACart/FTACart",	
		//"FTAProducts/FTAProducts"
	));	
	
	class MAIOrderItem extends FTAOrderItem {
		
		function MAIOrderItem(){
			parent::__construct();
			
			
			$this->keys = array_merge_recursive(
				array(				
					EXTRACT_NUMBER =>array("quantity"),//"days"),
					//EXTRACT_STRING => array("shortdescription","rentaldate","handedness","variant")
					EXTRACT_STRING => array("productId", "formMailerDataForOrderItem", "formData", "formMailerId", "member")
				),
				$this->keys
			);			
					
			
			$this->dataModelKeyMapping = array_merge(
				$this->dataModelKeyMapping,
				array(
					"formMailerDataForOrderItem" => "authordepartment",
					"productId" => "shortdescription",
					"formData" => "organisationalname",
					"formMailerId" => "authorbio",
					"member" => "address",
				));	
			
			global $db;	
			$this->cmsDocument = new CmsDocument($db);
		}
		
		
		var $_product;
		var $_masterProduct;
		
		//will need to apply discounts here..
		function price(){

			$product =  $this->getProduct();
			//echo "this member = <pre>";print_r($this->valueForKey('member'));echo "</pre>";
			
			$formData = unserialize($this->valueForKey('formMailerDataForOrderItem'));
			
			//echo "form data = <pre>";print_r($formData);echo "</pre>";
			
			if (null != $product){
				if($formData['member'] == 'true' && ($product->documenttypecode == 'docClasses' || $product->documenttypecode == 'docExhibit')){
					return $product->extra15;
				} else {
					return $product->extra14;
				}
			}
			return  0;
		}
		
		
		//work out the shipping prices..
		function shippingPrice() {
			$shippingPrice = '0.00';
			$product = $this->getProduct();
			
			if($product->documenttypecode == 'docArtwork'){
				$shippingPrice = "TBA";
			}
			
			
			return $shippingPrice;
		}
		
		
		//function getProductObject
		function getProduct() {
			if (is_null($this->_product)){
				$product = $this->cmsDocument->openDocument($this->valueForKey("productId"));
				$this->_product = $product;
			}
			return $this->_product;
		}
			
		/**
		 * the name of the product
		 * @return String
		 */
		function getProductName() {
			$product =  $this->getProduct();

			if (null != $product)
				return $product->title;
		}
		
		/**
		 * gets a description for the product
		 * @return String
		 */
		function getProductDescription(){
			$product =  $this->valueForKey('product');
			if (null != $product)
				return $product->shortdescription;
		}
		

		
		/**
		 * returns a url for an image to be displayed in the invoice
		 * @return  String the url for the image
		 */
		function getProductInvoiceImageUrl() {
			return $this->getProductThumbnailImageUrl();
			
			
		}
				
		/**
		 * returns a url for the product thumbnail image
		 * @return String the url for the image
		 */
		function getProductThumbnailImageUrl() {
			$product =  $this->getProduct();
			
			if (!(is_null($product->extra11))) {
				return  "/html/blob.php?documentCode=" . $product->documentcode . "&filetypecode=" . 4 . "&elementId=20200";
			} else {
				return "/images/placeholderInvoice.gif";
			}
		}
		
		/**
		 * returns a url for a large product image
		 * @return String the products large image url
		 */
		function getProductLargeImageUrl() {
			$product =  $this->getProduct();
			
			$firstImage = $product->extra11;
			if ($firstImage) {
				return  "/html/blob.php?documentCode=" . $product->documentcode . "&filetypecode=" . 2 . "&elementId=20200";
			} else {
				return "/images/placeholderInvoice.gif";
			}
		}
		
		/**
		 * returns an entry point into the site where you can access the products details
		 * @return String a url for the product in the site
		 */
		function getProductUrl(){
			$product =  $this->getProduct();			
			
			if($product->documenttypecode == "docArtwork"){
				$productURL = "/Artworks-For-Sale/" . urlFragmentClean($product->title) . "/";
			}
			if($product->documenttypecode == "docClasses"){
				$productURL = "/Classes/";
			}
			if($product->documenttypecode == "docExhibit"){
				$productURL = "/Exhibitions/";
			}
			if($product->documenttypecode == "docMember"){
				$productURL = "/Membership/";
			}
			if($product->documenttypecode == "docDonation"){
				$productURL = "/Donations-Bequests/";
			}
			
			return $productURL;
		}		
			
		function getProductCartDescription() {
			$product = $this->getProduct();
			$cartDisplay = '<span class="cartTitle">' . $this->product->title;
			if($product->documenttypecode == 'docArtwork' ){
				$cartDisplay .= ' - ' . $product->shortdescription . " ";
			} elseif($product->documenttypecode == 'docClasses'){
				$cartDisplay .= ' - ' . $product->shortdescription . " ";
			}elseif($product->documenttypecode == 'docExhibit') {
				$cartDisplay .= ' - ' . $product->authordepartment . " ";
			} elseif($product->documenttypecode == 'docMember'){
				$cartDisplay .= ' - Membership ';
			} elseif($product->documenttypecode == 'docDonation'){
				$cartDisplay .= ' - Donation ';
			}
			$cartDisplay .= "</span>";
			return $cartDisplay;
		}
		
		function getProductInvoiceDescription() {
			$product =  $this->getProduct();
			//$cartDisplay = $product->title;
			$cartDisplay = '<span class="cartItemDescription">';
			
			if($product->documenttypecode == 'docArtwork' ){
				$cartDisplay .= ' - ' . $product->shortdescription . " ";
			} elseif($product->documenttypecode == 'docClasses'){
				$cartDisplay .= ' - ' . $product->shortdescription . " ";
			}elseif($product->documenttypecode == 'docExhibit') {
				$cartDisplay .= ' - ' . $product->authordepartment . " ";
			} elseif($product->documenttypecode == 'docMember'){
				$cartDisplay .= ' - Membership ';
			} elseif($product->documenttypecode == 'docDonation'){
				$cartDisplay .= ' - Donation ';
			}
			
			return $cartDisplay . "</span>";
		}
			
	}